import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:gptompay_plugin/gptompay_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
    testRun();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> testRun() async {
    var packageName =
        kDebugMode ? "com.globalpayments.atom.dev" : "com.globalpayments.atom";

    var bindServiceResult =
        await GPtomPayPlugin.bindServer(gptomPackageName: packageName);
    print(bindServiceResult?.toJson());

    await Future.delayed(const Duration(seconds: 2));

    var registerV2Result = await GPtomPayPlugin.registerV2();
    if (registerV2Result != null) {
      if (registerV2Result.resultCode != 0) {
        print('registerV2Result--->${registerV2Result.responseMessage}');
      } else {
        print('registerV2Result--->${registerV2Result.toJson()}');
        var transactionId = registerV2Result.transactionId;
        print('transactionId--->$transactionId');
        var startV2Result = await GPtomPayPlugin.startV2(
          amount: 100,
          transactionID: transactionId,
        );
        if (startV2Result?.result != 0) {
          print('startV2Result--->${startV2Result?.responseMessage}');
        } else {
          print('startV2Result--->${startV2Result?.toJson()}');
        }

        Future.delayed(const Duration(seconds: 2), () async {
          var getStateResult = await GPtomPayPlugin.getState(
            transactionId: transactionId,
          );
          print('getStateResult--->${getStateResult?.toJson()}');
        });
        Future.delayed(const Duration(seconds: 2), () async {
          var getTransactionDetailResult =
              await GPtomPayPlugin.getTransactionDetail(
            transactionId: transactionId,
          );
          print(
              'getTransactionDetailResult--->${getTransactionDetailResult?.toJson()}');
        });
      }
    }
    await Future.delayed(const Duration(seconds: 2));

    await GPtomPayPlugin.unbindServer();

    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          // child: Text('Running on: $_platformVersion\n'),
          child: Column(
            children: [],
          ),
        ),
      ),
    );
  }
}
